#include "runnable_thread.h"

using namespace VLRTSP;

/**
 * @Author: CimZzz
 * @Date: 2022-01-12 09:13:57
 * 
 * 构造函数
 * @param runnable 线程执行函数回调
 */
RunnableThread::RunnableThread(Runnable runnable): thread(&RunnableThread::_threadRun, this, runnable) {
	thread.detach();
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-12 09:16:04
 * 
 * 析构函数
 */
RunnableThread::~RunnableThread() {
	// 等待线程结束
	try {
		if(this->thread.joinable()) {
			VL_PRINT("waitttttttt")
			this->thread.join();
		}
	}
	catch(std::system_error error) {
		// join error, if thread is done, maybe throw this error, ignore
	}
}

/**
 * @Author: CimZzz
 * @Date: 2022-01-12 09:14:46
 * 
 * 线程回调函数
 * @param runnable 线程执行函数回调
 */
void RunnableThread::_threadRun(Runnable runnable) {
	runnable();
}